package com.woniu108.evaluate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu108.carService.commos.dto.StoreDto;
import com.woniu108.carService.commos.exception.WoniuException;
import com.woniu108.carService.commos.exception.evaluate.EvaluateException;
import com.woniu108.carService.commos.exception.evaluate.EvaluateExceptionCode;
import com.woniu108.carService.commos.exception.orderProduct.OrderProductException;
import com.woniu108.carService.commos.exception.orderProduct.OrderProductExceptionCode;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.StaticData;
import com.woniu108.evaluate.client.OrderClient;
import com.woniu108.evaluate.client.PersonClient;
import com.woniu108.evaluate.dto.ProductScoreDto;
import com.woniu108.evaluate.dto.StoreScoreDto;
import com.woniu108.evaluate.model.ProductScore;
import com.woniu108.evaluate.model.StoreScore;
import com.woniu108.evaluate.mapper.StoreScoreMapper;
import com.woniu108.evaluate.param.StoreScoreAddParam;
import com.woniu108.evaluate.service.StoreScoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu108.order.model.dto.OrderServiceDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 门店评分表 服务实现类
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@Service
public class StoreScoreServiceImpl extends ServiceImpl<StoreScoreMapper, StoreScore> implements StoreScoreService {
    @Resource
    private PersonClient personClient;
    @Resource
    private OrderClient orderClient;
    @Resource
    private StoreScoreMapper storeScoreMapper;
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Boolean addStoreScoreByStoreId(Integer storeId) {
        Result<StoreDto> storeDtoResult = personClient.getStoreById(storeId);
        if(storeDtoResult.getCode()!=200){
            throw new WoniuException(storeDtoResult.getMsg(),storeDtoResult.getCode());
        }
        StoreDto storeDto = storeDtoResult.getData();
        //判断门店是否存在
        if(storeDto==null){
            throw new EvaluateException(EvaluateExceptionCode.EVALUATE_STORE_NOT_EXISTED);
        }
        //判断是否创建
        StoreScore score = storeScoreMapper.selectById(storeId);
        if(score!=null){
            throw new EvaluateException(EvaluateExceptionCode.EVALUATE_SCORE_EXISTED);
        }

        StoreScore storeScore = new StoreScore();
        storeScore.setStoreId(storeId);
        storeScore.setStoreScoreNum(0);
        storeScore.setStoreScore(BigDecimal.valueOf(0));
        storeScore.setStoreTotalScore(BigDecimal.valueOf(0));
        //执行创建
        storeScoreMapper.insert(storeScore);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Boolean addScore(StoreScoreAddParam param) {
        //orderServiceNum查询订单
        OrderServiceDto orderServiceDto = orderClient.queryOrderServiceByOrderServiceNum(param.getOrderServiceNum()).getData();

        //判断订单是否存在
        if(orderServiceDto==null) {
            throw new OrderProductException(OrderProductExceptionCode.ORDER_PRODUCT_NOT_EXISTED);
        }
        //用户是否将该订单已经删除
        if(!orderServiceDto.getOrderServiceState().equals(StaticData.ORDER_PRODUCT_STATE_DELETED)){
            throw new OrderProductException(OrderProductExceptionCode.ORDER_PRODUCT_DELETED);
        }
        //判断订单所有用户与登录用户是否同一
        if(!orderServiceDto.getUserId().equals(param.getUserId())){
            throw new EvaluateException(EvaluateExceptionCode.EVALUATE_REVIEW_USER_NOT_POWER);
        }
        //判断订单是否能评分评价
        if(!orderServiceDto.getOrderServiceState().equals(StaticData.ORDER_SERVICE_STATE_FINISHED)){
            throw new EvaluateException(EvaluateExceptionCode.EVALUATE_SCORE_NOT_POWER);
        }
        //订单是否已评分过
        if(orderServiceDto.getReviewState().equals(StaticData.ORDER_PRODUCT_REVIEW_STATE_FINISHED)){
            throw new EvaluateException(EvaluateExceptionCode.EVALUATE_SCORE_FINISHED);
        }

        //开始执行评分
        StoreScore storeScore = storeScoreMapper.selectById(orderServiceDto.getStoreId());
        storeScore.setStoreScoreNum(storeScore.getStoreScoreNum()+1);
        storeScore.setStoreTotalScore(storeScore.getStoreTotalScore().add(param.getScore()));
        storeScore.setStoreScore(storeScore.getStoreTotalScore().divide(BigDecimal.valueOf(storeScore.getStoreScoreNum())));
        storeScoreMapper.updateById(storeScore);
        //修改订单评价状态
        orderClient.modifyOrderServiceReviewStateByOrderServiceNum(orderServiceDto.getOrderServiceNum());
        return true;
    }

    @Override
    public StoreScoreDto selectStoreScoreById(Integer storeId) {
        StoreScore storeScore = storeScoreMapper.selectById(storeId);
        if(storeScore==null){
            return null;
        }
        StoreScoreDto storeScoreDto = BeanUtil.toBean(storeScore, StoreScoreDto.class);
        Result<StoreDto> storeDtoResult = personClient.getStoreById(storeScoreDto.getStoreId());
        if(storeDtoResult.getCode()!=200){
            throw new WoniuException(storeDtoResult.getMsg(),storeDtoResult.getCode());
        }
        if(storeDtoResult.getData()!=null){
            storeScoreDto.setStoreDto(storeDtoResult.getData());
        }
        return storeScoreDto;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Boolean delStoreScore(Integer storeId) {
        Result<StoreDto> storeDtoResult = personClient.getStoreById(storeId);
        if(storeDtoResult.getCode()!=200){
            throw new WoniuException(storeDtoResult.getMsg(),storeDtoResult.getCode());
        }
        StoreDto storeDto = storeDtoResult.getData();
        //判断门店是否存在
        if(storeDto!=null&&storeDto.getStoreState().equals(StaticData.STORE_STATE_DELETE)){
            throw new EvaluateException(EvaluateExceptionCode.EVALUATE_STORE_EXISTED);
        }
        //判断是否创建
        StoreScore score = storeScoreMapper.selectById(storeId);
        if(score==null){
            throw new EvaluateException(EvaluateExceptionCode.EVALUATE_SCORE_NO_EXISTED);
        }
        //执行删除
        storeScoreMapper.deleteById(score.getStoreId());
        return true;
    }

    @Override
    public List<StoreScoreDto> selectAllStoreScore() {
        QueryWrapper<StoreScore> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("store_total_score");
        queryWrapper.orderByDesc("store_score_num");
        List<StoreScore> storeScores = storeScoreMapper.selectList(queryWrapper);
        if(storeScores.size()>0){
            ArrayList<StoreScoreDto> storeScoreDtos = new ArrayList<>();
            for (StoreScore storeScore:storeScores){
                StoreScoreDto storeScoreDto = selectStoreScoreById(storeScore.getStoreId());
                storeScoreDtos.add(storeScoreDto);
            }
            return storeScoreDtos;
        }else {
            return null;
        }
    }
}
